package com.happyscan138.makefun.Fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.happyscan138.makefun.BaseAdapter.my_release_question;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.adapter.myReleaseQuestionAdapter;
import com.happyscan138.makefun.questionDetailed;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static android.content.Context.MODE_PRIVATE;
import static com.happyscan138.makefun.Urls.Urls.base;
import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2016/12/7.
 */

public class myrelease_question extends Fragment {
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private String access_key;
    private List<my_release_question> listdata;
    private ListView listView_myRelease_question;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.my_release_question,null);
        client=new AsyncHttpClient();
        params=new RequestParams();
        preferences = getActivity().getSharedPreferences("UserInfo", MODE_PRIVATE);
        access_key=preferences.getString("UserId","");
        initView(view);
        getAdapterView();
        return view;
    }
    public void initView(View view){
        listView_myRelease_question=(ListView) view.findViewById(R.id.my_release_listview);
    }

    public View getAdapterView(){
        View view=null;
        params.put("access_key",access_key);
        params.put("page_count","10");
        params.put("problem_id_index","0");
        RequestHandle handle=client.post(Urls.MyRelease,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                String jsonStr=response.toString();
                log.e("jsonStr",jsonStr);
                listdata=new ArrayList<>();
                try {
                    JSONObject jsonObject=new JSONObject(jsonStr);
                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject objectData=jsonArray.getJSONObject(i);
                        log.e("objectData",objectData.toString());
                        my_release_question data=new my_release_question();
                        data.icon= base+objectData.getString("img_path");
//                        log.e("icon",data.icon.toString());
                        data.name=objectData.getString("nick_name");
                        data.time=objectData.getString("create_date");
                        data.text=objectData.getString("content");
                        data.image= base+objectData.getString("resource");

                        data.viewNum=objectData.getInt("read_count");
                        data.answerNum=objectData.getInt("replay_count");
                        data.commentNum=i;
                        data.goodNum=objectData.getInt("admire_count");
                        listdata.add(data);

                    }
                    log.e("listData",listdata.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                listView_myRelease_question.setAdapter(new myReleaseQuestionAdapter(getActivity(),listdata));
                listView_myRelease_question.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        startActivity(new Intent(getActivity(), questionDetailed.class));

                    }
                });

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                log.e("responseString",responseString);
            }
        });

        return view;
    }
}
